<?php
$this->breadcrumbs=array(
	'Distributions'=>array('index'),
	'Manage',
);

$this->menu=array(
//	array('label'=>'List Distribution', 'url'=>array('index')),
//	array('label'=>'Create Distribution', 'url'=>array('create')),
	array('label'=>'Manage Society', 'url'=>array('society/admin')),    
);

Yii::app()->clientScript->registerScript('search', "
$('.search-button').click(function(){
	$('.search-form').toggle();
	return false;
});
$('.search-form form').submit(function(){
	$.fn.yiiGridView.update('distribution-grid', {
		data: $(this).serialize()
	});
	return false;
});
");
?>

<h1>Manage Distributions</h1>

<?php if(Yii::app()->user->hasFlash('distribution')): ?>
    <div class="flash-notice">
            <?php echo Yii::app()->user->getFlash('distribution'); ?>
    </div>
<?php endif; ?>


<div class="form">
    <?php echo CHtml::beginForm(); ?>

    <div class="row">
        <?php echo CHtml::label('Distribution Date', 'distributiondate'); ?>
        <?php
            $this->widget('zii.widgets.jui.CJuiDatePicker', array(
                'name' => 'distributiondate',
                // additional javascript options for the date picker plugin
                'options' => array(
                    'showAnim' => 'fold',
                    'dateFormat'=>'dd/mm/yy',
                ),
                'htmlOptions' => array(
                    'style' => 'height:20px;'
                ),
                'value' => Yii::app()->dateFormatter->format('dd/MM/yyyy', isset(Yii::app()->session['distributiondate'])?Yii::app()->session['distributiondate'] : time())
            ));
        ?>
    </div>


    <div class="row submit">
        <?php //echo CHtml::submitButton(UserModule::t("Login")); ?>
        <?php
            echo CHTML::button('Choose', array('submit' => Yii::app()->createUrl("distribution/choosedate")));
        ?>
    </div>

    <?php echo CHtml::endForm(); ?>
</div><!-- form -->

<?php echo CHtml::link('Advanced Search','#',array('class'=>'search-button')); ?>
<div class="search-form" style="display:none">
<?php $this->renderPartial('_search',array(
	'model'=>$model,
)); ?>
</div><!-- search-form -->


<?php
echo CHtml::beginForm();
?>

<?php $this->widget('zii.widgets.grid.CGridView', array(
	'id'=>'distribution-grid',
	'dataProvider'=>$model->search(),
	'filter'=>$model,
	'columns'=>array(
		//'iddistribution',
                array(
                  'header'=>'Society',
                  'value'=> '$data->idsociety0->societyname',
                  'htmlOptions' => array('width' => '20%', 'style'=>'text-align:center'),                
                ),
               /* array(
                  'header'=>'Financial Year',
                  'value'=> '$data->idfinancialyear0->financialyear',
                  'htmlOptions' => array('width' => '20%', 'style'=>'text-align:center'),                
                ),*/
            
//		'idfinancialyear',
		'distributiondate',
//		'idsociety',
//		'idstockitem',
                array(
                  'header'=>'Stock Item',
                  'value'=> '$data->idstockitem0->itemname',
                  'htmlOptions' => array('width' => '20%', 'style'=>'text-align:center'),
                ),
                array(
                    'header' => 'Demand Qty',
                    'value' => '$data->demandquantity',
                    'type' => 'raw',
                    'htmlOptions' => array('width' => '15%', 'style'=>'text-align:center'),
                ),                         
                array(
                    'header' => 'Available Qty',
                    'value' => 'CHTML::textField("availabilityquantity[$data->iddistribution]",$data->availabilityquantity,array(\'width\'=>20,\'maxlength\'=>30,\'style\'=>\'text-align:right\',\'onblur\'=>\'getTotal(\'.$data->iddistribution.\');\' ))',
                    'type' => 'raw',
                    'htmlOptions' => array('width' => '15%', 'style'=>'text-align:center'),
                ),   
                array(
                    'header' => 'Distribution Qty',
                    'value' => 'CHTML::textField("distributionquantity[$data->iddistribution]",$data->distributionquantity,array(\'width\'=>20,\'maxlength\'=>30,\'style\'=>\'text-align:right\',\'onblur\'=>\'getTotal(\'.$data->iddistribution.\');\' ))',
                    'type' => 'raw',
                    'htmlOptions' => array('width' => '15%', 'style'=>'text-align:center'),
                ),       
                array(
                    'header' => 'Total',
//                    'value' => 'CHTML::textField("total_$data->iddistribution",$data->total,array(\'width\'=>20,\'maxlength\'=>30,\'style\'=>\'text-align:right\'))',
                    'value' => '\'<span id=total_\' . $data->iddistribution . \'>\'.$data->total.\'</span>\'',
                    'type' => 'raw',
                    'htmlOptions' => array('width' => '15%', 'style'=>'text-align:center;font-weight:bold;'),
                ),                         
            
//		'distributionquantity',
//		'availabilityquantity',
		array(
			'class'=>'CButtonColumn',
                        'template' => '',                    
		),
	),
)); 
echo CHTML::button('Update Distribution', array('submit' => Yii::app()->createUrl("distribution/groupupdate")));
echo CHtml::endForm();

?>
<script>
        function getTotal(id){
            //getElementById("distributionquantity_56");
            $('#total_' + id).text($('#availabilityquantity_' + id).val() - $('#distributionquantity_' + id).val() );
//            alert($('#distributionquantity_' + id).val());
        }
</script>
